from typing import List
import json

# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children


class Solution:
    # 方法一：递归
    def postorder(self, root: 'Node') -> List[int]:
        ans = []
        def dfs(node: 'Node'):
            if node is None:
                return
            for ch in node.children:
                dfs(ch)
            ans.append(node.val)
        dfs(root)
        return ans

    # 方法三：迭代(优化后，再理解)
    def postorder3(self, root: 'Node') -> List[int]:
        if root is None:
            return []
        ans = []
        st = [root]
        vis = set()
        while st:
            node = st[-1]
            # 如果当前节点为叶子节点或者当前节点的子节点已经遍历过
            if len(node.children) == 0 or node in vis:
                ans.append(node.val)
                st.pop()
                continue
            st.extend(reversed(node.children))
            vis.add(node)
        return ans

    # 方法四：利用前序遍历反转
    def postorder4(self, root: 'Node') -> List[int]:
        if root is None:
            return []
        ans = []
        st = [root]
        while st:
            node = st.pop()
            ans.append(node.val)
            st.extend(node.children)
        ans.reverse()
        return ans


def stringToNode(input):
    input = input.strip()
    input = input[1:-1]
    if not input:
        return None

    inputValues = [s.strip() for s in input.split(',')]
    print("inputValues: ", inputValues)
    root = Node(int(inputValues[0]))
    nodeQueue = [root]
    front = 0
    index = 1
    while index < len(inputValues):
        node = nodeQueue[front]
        front = front + 1

        item = inputValues[index]
        # None 做分割符号
        index = index + 1
        if item != "null":
            valNumber = int(item)
            node.val = Node(valNumber)
            nodeQueue.append(node.val)

        if index >= len(inputValues):
            break

        item = inputValues[index]
        index = index + 1
        if item != "null":
            childrenNumber = int(item)
            node.children = Node(childrenNumber)
            nodeQueue.append(node.children)

    return root

    # TypeError: 'Node' object is not iterable
    # 字符串转N叉树怎么转？？？？


def  integerListToString(nums, len_of_list=None):
    if not len_of_list:
        len_of_list = len(nums)
    return json.dumps(nums[:len_of_list])


def main():
    import sys
    import io
    def readlines():
        for line in io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8'):
            yield line.strip('\n')

    lines = readlines()
    while True:
        try:
            line = next(lines)
            nums = json.loads(line)
            print(nums)

            # root = int(line)
            # print(type(root))

            # # TypeError: 'Node' object is not iterable
            root = stringToNode(line)
            print(type(root))

            ret = Solution().postorder(root)

            out = integerListToString(ret)
            print(out)
        except StopIteration:
            break

if __name__ == '__main__':
    main()
    
    """
    怎么转为N叉树???
    """